package com.google.code.roadmap.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.apache.log4j.Logger;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
import org.springframework.stereotype.Repository;

import com.google.code.roadmap.dao.RoadMapDao;
import com.google.code.roadmap.dao.sqlparser.impl.SqlParserXmlImpl;
import com.google.code.roadmap.web.domain.RoadMap;

@Repository("roadMapJdbcDao")
public class RoadMapDaoJdbcImpl extends SimpleJdbcDaoSupport implements
		RoadMapDao {

	@Resource(name = "SqlParser")
	SqlParserXmlImpl sqlparser;
	
	// 로그 표시를 위하여
	private Logger log = Logger.getLogger(this.getClass());

	@Resource(name = "dataSource")
	public void setJdbcDao(DataSource dataSource) {
		setDataSource(dataSource);
	}

	@Override
	public RoadMap getRoadMap(Integer idNum) {
		
		log.info(idNum);

		String sql = sqlparser.getSQL("roadmap", "GET_ROADMAP_SQL");

		return getSimpleJdbcTemplate().queryForObject(sql,

		new ParameterizedRowMapper<RoadMap>() {
			@Override
			public RoadMap mapRow(ResultSet rs, int rowNum) throws SQLException {

				RoadMap roadMap = new RoadMap();
				roadMap.setNorth(new RoadMap());
				roadMap.setSouth(new RoadMap());
				roadMap.setEast(new RoadMap());
				roadMap.setWest(new RoadMap());
				roadMap.setNw(new RoadMap());
				roadMap.setNe(new RoadMap());
				roadMap.setSe(new RoadMap());
				roadMap.setSw(new RoadMap());

				roadMap.setIdNum(rs.getInt("idNum"));
				roadMap.setFileName(rs.getString("filename"));
				roadMap.getNorth().setIdNum(rs.getInt("north"));
				roadMap.getSouth().setIdNum(rs.getInt("south"));
				roadMap.getEast().setIdNum(rs.getInt("east"));
				roadMap.getWest().setIdNum(rs.getInt("west"));
				roadMap.getNw().setIdNum(rs.getInt("nw"));
				roadMap.getNe().setIdNum(rs.getInt("ne"));
				roadMap.getSe().setIdNum(rs.getInt("se"));
				roadMap.getSw().setIdNum(rs.getInt("sw"));

				roadMap.setComment(rs.getString("comment"));

				return roadMap;
			}
		}, new Object[] { idNum });
	}

}
